
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.ImageIcon.*;
import java.text.NumberFormat;

/*
 * This class represents the start panel
 */

public class StartPanel extends JPanel{
		private Graphics canvas;
		private JFormattedTextField splitCountField = new JFormattedTextField(NumberFormat.getInstance());
		private JFormattedTextField truncationBiasField = new JFormattedTextField(NumberFormat.getInstance());
		private JFormattedTextField thresholdHitsField = new JFormattedTextField(NumberFormat.getInstance());
		int levelHeight=0;
		public int currentLevel=0;
		private JPanel parPanel;
		private JLabel statusBar;
		
		//constructor
	    public StartPanel() {

	        parPanel = new JPanel();
	        SplitSim.parPanel=parPanel;
	        
	        parPanel.setPreferredSize(new Dimension(900, 70));
	        
	        JLabel splitCountLabel = new JLabel("Number of Splits: ");
			splitCountField.setColumns(5);
			splitCountField.setValue(100);
	        splitCountField.setLocation(100, 100);
	        parPanel.add(splitCountLabel);
            parPanel.add(splitCountField);
            
            JLabel truncationBiasLabel = new JLabel("\n\n Truncation Bias: ");
			truncationBiasField.setColumns(5);
			truncationBiasField.setValue(3);
	        parPanel.add(truncationBiasLabel);
            parPanel.add(truncationBiasField);
            
            JLabel thresholdHitsLabel = new JLabel("Threshold Hits/Split: ");
			thresholdHitsField.setColumns(5);
			thresholdHitsField.setValue(10);
	        parPanel.add(thresholdHitsLabel);
            parPanel.add(thresholdHitsField);
            
            JLabel space = new JLabel("                  ");
            parPanel.add(space); 
            
            JButton startButton = new JButton("Start Simulation");
            parPanel.add(startButton); 
            
	        add(parPanel);
	        
	        startButton.addActionListener(
	        		new ActionListener(){
	        			public void actionPerformed(ActionEvent e){
	        	    		
	        				SplitSim.splitCount=((Number)splitCountField.getValue()).intValue();
	        				SplitSim.truncationBias=((Number)truncationBiasField.getValue()).intValue();
	        				SplitSim.thresholdHits=((Number)thresholdHitsField.getValue()).intValue();
	        				paintComponent(getGraphics());
	        				startEngine();
	        				
	        			}
	        		}
	        	 );
	    }
	    
	    //starts the simulation engine
	    private void startEngine(){
	    	Engine engine = new Engine();
			engine.run();
	    }
	    
	    //prepares the realtime simulation display
	    public void paintComponent(Graphics canvasGraphics){
	    	SplitSim.canvas=canvasGraphics;
			canvas = canvasGraphics;
			canvas.setColor(Color.black);
			canvas.fillRect(50, 80, 900, 300);
			
			this.setPreferredSize(new Dimension(1000, 500));
			int xStart=50;
			int yStart=80+300;
			int xEnd=950;
			int yEnd=80;
			levelHeight=300/SplitSim.splitCount;
			int levelY=yStart;
			if(levelHeight>=1){
				canvas.setColor(Color.gray);
				for(int runIndex=1;(runIndex<SplitSim.splitCount || levelY>yEnd);runIndex++ ){
					levelY=levelY-levelHeight;
					canvas.drawLine(xStart, levelY, xEnd, levelY);
				}
			}
		}
	    
}
